<?php declare( strict_types=1 );
use PHPUnit\Framework\TestCase;

// For suppressing "Undefined index" notices
$_SERVER['REQUEST_SCHEME'] = '';
$_SERVER['HTTP_HOST'] = '';
$_SERVER['SCRIPT_NAME'] = '';

// We would have to include the file manually since composer won't be
// able to import it because it's not class.
require_once( dirname( __DIR__ ) . '/inc/common.php' );


final class CommonTest extends TestCase
{
	private $test_base_url;
	
	protected function setUp(): void
	{
        // For testing code where $_SERVER variables are needed
        $_SERVER['REQUEST_SCHEME'] = 'http';
        $_SERVER['HTTP_HOST'] = 'localhost';
        $_SERVER['SCRIPT_NAME'] = '/vioscope/index.php';
        
        // Stores test base_url
        $this->test_base_url = 'http://localhost/vioscope';
        
        parent::setUp();
	}

	public function test_get_base_url(): void
	{
		// Unset config to test auto-detect base_url (through $_SERVER)
		unset( $GLOBALS['config']['base_url'] );
		$this->assertEquals(
			$this->test_base_url,
			get_base_url()
		);
		
		// Test if the $config value for base_url is respected
		$GLOBALS['config']['base_url'] = 'https://example.com';
		$this->assertEquals(
			'https://example.com',
			get_base_url()
		);
		
		// Unset for later tests
		unset( $GLOBALS['config']['base_url'] );
	}
	
	public function test_get_local_url(): void
	{
		// Test local video url generation
		$this->assertEquals(
			'http://localhost/vioscope/watch?v=C0DPdy98e4c',
			get_local_url( 'C0DPdy98e4c', 'video' )
		);

		// Test local channel url generation
		$this->assertEquals(
			'http://localhost/vioscope/channel/UCHDm-DKoMyJxKVgwGmuTaQA',
			get_local_url( 'UCHDm-DKoMyJxKVgwGmuTaQA', 'channel' )
		);

	}
	
	public function test_replace_urls_with_local_urls(): void
	{
		$this->assertEquals(
			$this->test_base_url . '/watch?v=C0DPdy98e4c&feature=feedrec_grec_index',
			replace_urls_with_local_urls( 'https://www.youtube.com/watch?v=C0DPdy98e4c&feature=feedrec_grec_index' )
		);
		$this->assertEquals(
			$this->test_base_url . '/watch?v=C0DPdy98e4c',
			replace_urls_with_local_urls( 'http://youtu.be/C0DPdy98e4c' )
		);
		$this->assertEquals(
			$this->test_base_url . '/watch?v=C0DPdy98e4c?rel=0',
			replace_urls_with_local_urls( 'https://www.youtube.com/embed/C0DPdy98e4c?rel=0' )
		);
		$this->assertEquals(
			$this->test_base_url . '/user/SomeUser_Name#p/a/u/1/QdK8U-VIH_o',
			replace_urls_with_local_urls( 'https://www.youtube.com/user/SomeUser_Name#p/a/u/1/QdK8U-VIH_o' )
		);
		$this->assertEquals(
			$this->test_base_url . '/channel/UCHDm-DKoMyJxKVgwGmuTaQA',
			replace_urls_with_local_urls( 'https://youtube.com/channel/UCHDm-DKoMyJxKVgwGmuTaQA' )
		);
	}
	
	public function test_urlify_string(): void
	{
		$this->assertEquals(
			'Example <a href="https://www.youtube.com/watch?v=C0DPdy98e4c">https://www.youtube.com/watch?v=C0DPdy98e4c</a> text',
			urlify_string( 'Example https://www.youtube.com/watch?v=C0DPdy98e4c text' )
		);
		$this->assertEquals(
			'Example <a href="http://192.168.0.2/watch?v=C0DPdy98e4c">http://192.168.0.2/watch?v=C0DPdy98e4c</a>',
			urlify_string( 'Example http://192.168.0.2/watch?v=C0DPdy98e4c' )
		);
	}
	
	public function test_get_video_thumbnail_url(): void
	{
		$this->assertEquals(
			'https://i.ytimg.com/vi/C0DPdy98e4c/mqdefault.jpg',
			get_video_thumbnail_url( 'C0DPdy98e4c' )
		);
		$this->assertEquals(
			'https://i.ytimg.com/vi/C0DPdy98e4c/default.jpg',
			get_video_thumbnail_url( 'C0DPdy98e4c', 'small' )
		);
		$this->assertEquals(
			'https://i.ytimg.com/vi/C0DPdy98e4c/hqdefault.jpg',
			get_video_thumbnail_url( 'C0DPdy98e4c', 'high' )
		);
	}

}
